package com.zp.base.controller.printPdfTemplate;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.zp.base.entity.printPdfTemplate.bo.PrintPdfTemplateBo;
import com.zp.base.entity.printPdfTemplate.vo.PrintPdfTemplateVo;
import com.zp.base.service.printPdfTemplate.IPrintPdfTemplateService;
import com.zp.common.core.controller.BaseController;
import com.zp.common.core.entity.R;
import com.zp.common.core.utils.StringUtils;
import com.zp.common.core.validate.AddGroup;
import com.zp.common.core.validate.EditGroup;
import com.zp.common.excel.utils.ExcelUtil;
import com.zp.common.log.annotation.Log;
import com.zp.common.log.enums.BusinessType;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import com.zp.common.oss.config.FileConfig;
import com.zp.common.oss.config.uploadMessage;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.websocket.server.PathParam;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * pdf打印模板字典控制器
 * 前端访问路由地址为:/base/printPdfTemplate
 *
 * @author zhang peng
 * @ date 2023-09-18
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/printPdfTemplate")
public class PrintPdfTemplateController extends BaseController {

    private final IPrintPdfTemplateService iPrintPdfTemplateService;

    /**
     * 查询pdf打印模板字典列表 (分页)
     */
    @SaCheckPermission("base:printPdfTemplate:list")
    @GetMapping("/selectPage")
    public TableDataInfo<PrintPdfTemplateVo> selectPage (PrintPdfTemplateBo bo, PageQuery pageQuery) {
        return iPrintPdfTemplateService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询pdf打印模板字典列表 (不分页)
     */
    @SaCheckPermission("base:printPdfTemplate:list")
    @GetMapping("/list")
    public R<List<PrintPdfTemplateVo>> list (PrintPdfTemplateBo bo) {
        List<PrintPdfTemplateVo> list = iPrintPdfTemplateService.queryList(bo);
        return R.ok(list);
    }

    /**
     * 导出pdf打印模板字典列表
     */
    @SaCheckPermission("base:printPdfTemplate:export")
    @Log(title = "pdf打印模板字典", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export (PrintPdfTemplateBo bo, HttpServletResponse response) {
        List<PrintPdfTemplateVo> list = iPrintPdfTemplateService.queryList(bo);
        ExcelUtil.exportExcel(list, "pdf打印模板字典", PrintPdfTemplateVo.class, response);
    }

    /**
     * 获取pdf打印模板字典详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("base:printPdfTemplate:list")
    @GetMapping("/{id}")
    public R<PrintPdfTemplateVo> getInfo (@NotNull(message = "主键不能为空") @PathVariable String id) {
        return R.ok(iPrintPdfTemplateService.queryById(id));
    }

    /**
     * 新增pdf打印模板字典
     */
    @SaCheckPermission("base:printPdfTemplate:add")
    @Log(title = "pdf打印模板字典", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add (@Validated(AddGroup.class) @RequestBody PrintPdfTemplateBo bo) {
        return toAjax(iPrintPdfTemplateService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改pdf打印模板字典
     */
    @SaCheckPermission("base:printPdfTemplate:edit")
    @Log(title = "pdf打印模板字典", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit (@Validated(EditGroup.class) @RequestBody PrintPdfTemplateBo bo) {
        return toAjax(iPrintPdfTemplateService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除pdf打印模板字典
     *
     * @param ids 主键串
     */
    @SaCheckPermission("base:printPdfTemplate:remove")
    @Log(title = "pdf打印模板字典", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove (@NotEmpty(message = "主键不能为空") @PathVariable String[] ids) {
        return toAjax(iPrintPdfTemplateService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }

    /**
     * @return
     * @Author zhangpeng
     * @Description //TODO pdf模板上传
     * @ date 14:17 2023/9/21
     * @Param * @param null
     **/
    @RequestMapping("/uploadPdfFile")
    @Log(title = "文件上传", businessType = BusinessType.OTHER)
    public R<uploadMessage> uploadPdfFile (@RequestParam("file") MultipartFile file,
                                           @PathParam(value = "templateName") String templateName,
                                           @PathParam(value = "templateCode") String templateCode,
                                           @PathParam(value = "templateType") String templateType,
                                           @PathParam(value = "orgId") String orgId,
                                           @PathParam(value = "filePath") String filePath
    ) throws IOException {
        uploadMessage uploadMessage = new uploadMessage();
        if (StringUtils.isEmpty(orgId)) {
            throw new RuntimeException("机构信息不能为空，请选择需要维护的机构。");
        }
        if (StringUtils.isEmpty(templateName)) {
            throw new RuntimeException("模板名称不能为空，请填写模板名称。");
        }
        if (StringUtils.isEmpty(templateType)) {
            throw new RuntimeException("模板类型不能为空，请选择模板类型后再上传。");
        }
        if (!file.isEmpty()) {
            uploadMessage = iPrintPdfTemplateService.uploadPdfFile(FileConfig.getStaticPath(), file, templateName, templateCode, templateType, orgId, filePath);
        }
        return R.ok(uploadMessage);
    }

    /**
     * @ Author zhangpeng
     * @ Description //TODO 报表模板下载（二进制）
     * @ Date 13:42 2023/10/19
     * @ return
     **/
    @PostMapping("/downloadFile")
    public void downloadFile (String id, HttpServletResponse response, HttpServletRequest request) {
        iPrintPdfTemplateService.downloadFile(id, response, request);
    }

    @PostMapping("/printTest")
    public void printTest (String id, HttpServletResponse response, HttpServletRequest request) {
        iPrintPdfTemplateService.printTest(id, request, response);
    }

}
